﻿/*****************************************
 * 
 *  copyright (c) by ylshen
 * 
 *  author : ylshen (syl.lucky@gmail.com)
 * 
 **************************************** */
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using JYStockAnalyser.Model;

namespace JYStockAnalyser.DA
{
    /// <summary>
    /// 数据精华映射器
    /// </summary>
    public class EssentialDataMapper : ORMapper
    {
        /// <summary>
        /// 获取最新的主力五日增仓数据(按增加金额排序)
        /// </summary>
        /// <returns></returns>
        public List<StockMarketInfo> GetLastFiveAddingOrderByAdding()
        {            
            string sql = "SELECT [order], stockNO, stockName, currentPrice, upORDownRange, tradingVolume, money, addOrSubtractMoney FROM t_fiveAdding WHERE [date] = (SELECT MAX([date]) FROM t_fiveAdding) ORDER BY [order]";
            return m_getInfos(sql);
        }

        /// <summary>
        /// 获取最新的主力五日增仓数据(按股票代码排序)
        /// </summary>
        /// <returns></returns>
        public List<StockMarketInfo> GetLastFiveAddingOrderByStockNO()
        {
            string sql = "SELECT [order], stockNO, stockName, currentPrice, upORDownRange, tradingVolume, money, addOrSubtractMoney FROM t_fiveAdding WHERE [date] = (SELECT MAX([date]) FROM t_fiveAdding) ORDER BY stockNO";
            return m_getInfos(sql);
        }

        /// <summary>
        /// 获取最新的主力五日减仓数据(按增加金额排序)
        /// </summary>
        /// <returns></returns>
        public List<StockMarketInfo> GetLastFiveSubtractingOrderBySubtracting()
        {
            string sql = "SELECT [order], stockNO, stockName, currentPrice, upORDownRange, tradingVolume, money, addOrSubtractMoney FROM t_fiveSubtracting WHERE [date] = (SELECT MAX([date]) FROM t_fiveSubtracting) ORDER BY [order]";
            return m_getInfos(sql);
        }

        /// <summary>
        /// 获取最新的主力五日减仓数据(按股票代码排序)
        /// </summary>
        /// <returns></returns>
        public List<StockMarketInfo> GetLastFiveSubtractingOrderByStockNO()
        {
            string sql = "SELECT [order], stockNO, stockName, currentPrice, upORDownRange, tradingVolume, money, addOrSubtractMoney FROM t_fiveSubtracting WHERE [date] = (SELECT MAX([date]) FROM t_fiveSubtracting) ORDER BY stockNO";
            return m_getInfos(sql);
        }

        private List<StockMarketInfo> m_getInfos(string sql)
        {
            List<StockMarketInfo> infos = new List<StockMarketInfo>();
            using (IDataReader reader = Excute_Reader(sql))
            {
                while (reader.Read())
                {
                    StockMarketInfo info = new StockMarketInfo();
                    info.Order = GetReaderInt32Value(reader, 0);
                    info.StockNO = GetReaderString(reader, 1);
                    info.StockName = GetReaderString(reader, 2);
                    info.CurrentPrice = GetReaderDecimalValue(reader, 3);              
                    info.UpORDownRange = GetReaderDecimalValue(reader, 4);
                    info.TradingVolume = GetReaderInt32Value(reader, 5);
                    info.Money = GetReaderDecimalValue(reader, 6);
                    info.AddOrSubtractMoney = GetReaderDecimalValue(reader, 7);
                    infos.Add(info);
                }
            }
            return infos;
        }
    }
}
